package common;

import java.io.PrintStream;
import java.util.Vector;



public class LogPrinter {
	private Vector<InfoElement> printInfoList = new Vector<InfoElement>();
	private PrintStream writer = null;
	
	public LogPrinter( PrintStream writer ){
		this.writer = writer;
	}
	
	public void printScannerTrace( Token token, String lexeme ,int lineNum){		
		printInfoList.add( new InfoElement(token, lexeme) );		
	}
	
	public void printParserTrace( String info ){
		printInfoList.lastElement().addParserInfo( info );
	}
	
	public void printInputCode( String inputCode, int lineNum ){
		String line = String.valueOf( lineNum );		
		if( inputCode.length()>0 && inputCode.charAt( inputCode.length()-1 ) != (int)'\n' ){
			inputCode+='\n';
		}
		
		//when get user Source Code, print immediately
		if( writer!=null ){
			writer.print( line + ":" + inputCode );		
		
			for( int i=0; i<printInfoList.size(); i++ ){			
				printInfoList.get(i).printToLog(writer, lineNum);
				writer.print('\n');
			}
		}
		
		clear();
	}
	
	public void println(String s){
		writer.print(s+'\n');
	}
	
	private void clear(){
		printInfoList.clear();
	}
	
	public void flush(){
		writer.flush();
	}
	
	public void close(){
		writer.flush();
		writer.close();
	}
}
